<template>
  <el-container style="height: 600px; border: 1px solid #eee">
    <el-header style="font-size: 40px; background-color: rgb(238, 241, 246)">国密算法工具箱 - SM3 哈希计算</el-header>
    <el-container>
      <!-- 左侧 菜单栏 -->
      <el-aside width="230px" style="background-color: rgb(238, 241, 246); border: 1px solid #ddd;">
        <el-menu default-active="1-1">
          <el-submenu index="1">
            <template slot="title">加密解密工具</template>
            <el-menu-item index="1-1">
                <router-link to="/sm2">SM2</router-link>
              </el-menu-item>
            <el-menu-item index="1-1" @click="switchTab('text')">文本加密解密</el-menu-item>
            <el-menu-item index="1-2" @click="switchTab('image')">图片加密解密</el-menu-item>
              <el-menu-item index="1-2">
                <router-link to="/sm3">SM3</router-link>
              </el-menu-item>
              <el-menu-item index="1-3">
                <router-link to="/sm4">SM4</router-link>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧 内容 -->
      <el-main style="padding: 20px;">
        <div>
          <h1>SM3 哈希计算</h1>

          <!-- 输入明文 -->
          <el-input v-model="text" placeholder="输入明文" style="margin-bottom: 15px;"></el-input>

          <!-- 操作按钮组 -->
          <el-button @click="handleHash" :loading="loading" type="primary" style="margin-right: 10px;">计算哈希</el-button>
          <el-button @click="clearInput" type="warning" style="margin-right: 10px;">清除</el-button>
          <el-button v-if="hashValue" @click="copyToClipboard" type="success">复制哈希</el-button>

          <!-- 显示哈希值 -->
          <p v-if="hashValue" style="margin-top: 10px;">哈希值: {{ hashValue }}</p>

          <!-- 历史记录和操作 -->
          <div v-if="hashHistory.length > 0" style="margin-top: 20px;">
            <h3>哈希计算历史</h3>
            <el-button @click="clearHistory" type="danger" size="mini" style="margin-bottom: 10px;">清除历史</el-button>
            <el-table :data="hashHistory" style="width: 100%">
              <el-table-column prop="text" label="明文"></el-table-column>
              <el-table-column prop="hash" label="哈希值"></el-table-column>
              <el-table-column label="操作">
                <template #default="scope">
                  <el-button @click="copyHistoryHash(scope.row.hash)" type="text" size="mini">复制</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { sm3 } from 'sm-crypto';

export default {
  data() {
    return {
      text: '',
      hashValue: '',
      hashHistory: [],
      loading: false
    };
  },
  methods: {
    handleHash() {
      if (!this.text) {
        this.$message.error("请输入明文");
        return;
      }
      this.loading = true;
      setTimeout(() => {  // Simulate loading time if needed
        const hashValue = sm3(this.text);
        this.hashValue = hashValue;
        this.hashHistory.push({ text: this.text, hash: hashValue });
        this.loading = false;
        this.$message.success("哈希计算成功");
      }, 500);
    },
    clearInput() {
      this.text = '';
      this.hashValue = '';
    },
    copyToClipboard() {
      navigator.clipboard.writeText(this.hashValue).then(() => {
        this.$message.success("哈希值已复制到剪贴板");
      });
    },
    clearHistory() {
      this.hashHistory = [];
      this.$message.success("历史记录已清除");
    },
    copyHistoryHash(hash) {
      navigator.clipboard.writeText(hash).then(() => {
        this.$message.success("历史哈希值已复制到剪贴板");
      });
    }
  }
};
</script>

<style scoped>
h1 {
  color: #333;
}
p {
  word-wrap: break-word;
  word-break: break-all;
  background-color: #f5f5f5;
  padding: 10px;
  border-radius: 4px;
  margin-top: 10px;
}
</style>
